package com.qjc.demo.data_structure.tree_demo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 查询“语文”，则输出[5,7]
 * @Author: qjc
 * @Date: 2020/3/19
 */
public class SchoolTest {

    public static void main(String[] args) {
        Department department = new Department();
        department.setId(1L);
        department.setName("学校");

        List<Department> departmentList = new ArrayList<>();

        Department department1 = new Department();
        department1.setId(2L);
        department1.setName("一年级");

        List<Department> departmentList1 = new ArrayList<>();
        Department department11 = new Department();
        department11.setId(5L);
        department11.setName("语文");
        departmentList1.add(department11);
        Department department12 = new Department();
        department12.setId(6L);
        department12.setName("数学");

        List<Department> departmentList4 = new ArrayList<>();
        Department department121 = new Department();
        department121.setId(9L);
        department121.setName("数学一组");
        departmentList4.add(department121);

        department12.setChildDepartMentList(departmentList4);

        departmentList1.add(department12);
        department1.setChildDepartMentList(departmentList1);

        departmentList.add(department1);

        Department department2 = new Department();
        department2.setId(3L);
        department2.setName("二年级");
        List<Department> departmentList2 = new ArrayList<>();
        Department department21 = new Department();
        department21.setId(7L);
        department21.setName("语文");
        departmentList2.add(department21);
        department2.setChildDepartMentList(departmentList2);

        departmentList.add(department2);

        Department department3 = new Department();
        department3.setId(4L);
        department3.setName("三年级");
        List<Department> departmentList3 = new ArrayList<>();
        Department department31 = new Department();
        department31.setId(8L);
        department31.setName("英语");

        departmentList3.add(department31);

        department3.setChildDepartMentList(departmentList3);

        departmentList.add(department3);

        department.setChildDepartMentList(departmentList);

        getSonDept("学校", department);


    }

    private static void getSonDept(String departmentName, Department department) {
        String name = department.getName();
        Long id = department.getId();
        if (name.equals(departmentName)) {
            System.err.println(id);
        } else {
            List<Department> childDepartMentList = department.getChildDepartMentList();
            if (childDepartMentList == null || childDepartMentList.size() == 0) {
                System.err.println(name + "没有子部门");
            } else {
                System.err.println(name + "有子部门");
                for (Department department1 : childDepartMentList) {
                    getSonDept(departmentName, department1);
                }
            }
        }
    }


}
